package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@Tag(name = "02-车辆模块")
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 1.添加车辆功能
     * @param vehicleAddParam 车辆信息DTO
     * @param session 会话参数
     * @return
     */
    @PostMapping("add")
    @Operation(summary = "添加车辆功能")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session){
        log.debug("vehicleAddParam = " + vehicleAddParam);
        /*
            1.从session中获取用户标识userVO; session.getAttribute("user");
            2.判断userVO是否为null;
            3.未登录状态:直接返回结束;
            4.登录状态,插入数据[复制属性、处理其他字段];
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO = " + userVO);
        //if (userVO == null) { // 未登录
        //    return new JsonResult(StatusCode.NOT_LOGIN);
        //}
        //添加车辆
        Vehicle vehicle = new Vehicle();
        //复制属性、处理其他字段、调用接口、返回响应
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        log.debug("vehicle = " + vehicle);

        vehicleMapper.insertVehicle(vehicle);

        return JsonResult.ok();
    }

    /**
     * 2.车辆列表
     */
    @GetMapping("list")
    @Operation(summary = "车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand", description = "车辆品牌"),
            @Parameter(name = "license", description = "车牌号"),
            @Parameter(name = "vehicleListQuery", hidden = true)
    })
    public JsonResult list(VehicleListQuery vehicleListQuery, HttpSession session){
        log.debug("vehicleListQuery = " + vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //未登录状态
        //if (userVO == null){
        //    return new JsonResult(StatusCode.NOT_LOGIN);
        //}
        //登录状态
        //SELECT ... FROM vehicle WHERE brand=? AND license=? AND user_id=?
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userVO.getId();
        List<VehicleListVO> vehicleListVOS = vehicleMapper.selectVehicleList(brand, license, userId);

        return JsonResult.ok(vehicleListVOS);
    }

    /**
     * 3.删除车辆
     */
    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult del(@Range(min = 1, max = 500, message = "车辆id必须在1-500之间") @PathVariable Long id, HttpSession session){
        //手动抛出异常,用于演示全局异常处理器
        if (id < 0){
            throw new IllegalArgumentException("id值不能小于0");
        }
        log.debug("id = " + id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //if (userVO == null){
        //    return new JsonResult(StatusCode.NOT_LOGIN);
        //}
        Long userId = userVO.getId();
        vehicleMapper.deleteVehicle(id, userId);

        return JsonResult.ok();
    }
}







